package com.ysd.crm.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.ysd.crm.entity.LayuiTree;
import com.ysd.crm.entity.Modules;
import com.ysd.crm.search.ModulesSearch;
import com.ysd.crm.service.ModulesService;
import com.ysd.crm.utils.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Tang
 *模块管理controller
 */
@RequestMapping("/modules")
@Api
@RestController
public class ModulesController {
	@Autowired
	private ModulesService MS;
	
	/**
	 * http://localhost:8080/modules/getTreeAll
	 */
	@ApiOperation("获取所有模块树-txh")
	@PostMapping("getTreeAll")
	public String getTreeAll(LayuiTree l) {
		List<LayuiTree> all = MS.getLayuiModuleTree(l);
		return Result.toJson(all); 
	}
	/**
	 * http://localhost:8080/modules/getAll
	 */
	@ApiOperation("获取所有模块树-txh")
	@PostMapping("getAll")
	public String getAll(LayuiTree l) {
		List<Modules> all = MS.getAll(l);
		return Result.toJson(all); 
	}
	
	/**
	 * @return
	 */
	@ApiOperation("添加模块-txh")
	@PostMapping("/add")
	public String add(ModulesSearch m) {
		Result res = MS.add(m);
		return Result.toJson(res);
	}
	
	@ApiOperation("修改模块-txh")
	@PostMapping("/update")
	public String update(ModulesSearch m) {
		Result res = MS.update(m);
		return Result.toJson(res);
	}
	
	@ApiOperation("删除模块-txh")
	@PostMapping("/delete")
	public String delete(ModulesSearch m) {
		Result res = MS.delete(m);
		return Result.toJson(res);
	}
	
	@ApiOperation("查询用户拥有的所有模块-txh")
	@PostMapping("/getModulesByUid")
	public String getModulesByUid(String uid) {
		List<Modules> list = MS.getModulesByUid(uid);
		return Result.toJson(list);
	}
	
	@ApiOperation("根据角色ID查询所有模块和用户拥有的模块-txh")
	@RequestMapping("getModuleByRid")
	public String getModulesByRid(String rid) {
		List<LayuiTree> tree = MS.getModuleByRid(rid);
		return Result.toJson(tree);
	}
	
	@RequestMapping("addModuleByRid")
	public String addModuleByRid(String rid,String mid) {
		Result result = MS.addRoleModulesByRidAndMids(rid, mid);
		return Result.toJson(result);
	}
	@RequestMapping("delModuleByRid")
	public String delModuleByRid(String rid,String mid) {
		Result result = MS.delRoleModulesByRidAndMids(rid, mid);
		return Result.toJson(result);
	}
}
